package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.impl.DeviceServiceImpl;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DeviceShadowDataVo;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 查询设备Controller
 * 
 * @author laoYe
 * @date 2025-10-28
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "查询设备相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取查询设备列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 同步产品列表
     * @return
     */
    @PostMapping("/syncProductList")
    @ApiOperation(value = "同步产品列表")
    public R syncProductList() {
        deviceService.syncProductList();
        return R.ok();
    }

    /**
     * 获取所有产品列表
     * @return
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "获取所有产品列表")
    public AjaxResult allProduct() {
        return AjaxResult.success(deviceService.allProduct());
    }

    /**
     * 新增IoT设备管理
     * @param deviceDto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "IoT设备管理",businessType = BusinessType.INSERT)
    @PostMapping("/register")
    public R add(@ApiParam(value = "IoT设备管理实体",required = true) @RequestBody DeviceDto deviceDto){
        deviceService.add(deviceDto);
        return R.ok();
    }

    /**
     * 获取设备详情信息
     */
    @GetMapping("/{iotId}")
    @ApiOperation("获取设备详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "设备ID", required = true, dataTypeClass = String.class)
    })
    public R<DeviceDetailVo> getInfo(@PathVariable("iotId") String iotId){
        return R.ok(deviceService.getInfo(iotId));
    }

    /**
     * 查询设备上报数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报的最新数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "设备ID", required = true, dataTypeClass = String.class)
    })
    public R<List<DeviceShadowDataVo>> queryServiceProperties(@PathVariable("iotId") String iotId){
        return R.ok(deviceService.queryServiceProperties(iotId));
    }

    /**
     * 修改设备
     * @param deviceDto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:update')")
    @Log(title = "修改设备",businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改设备")
    public R update(@RequestBody DeviceDto deviceDto){
        deviceService.update(deviceDto);
        return R.ok();
    }
}
